// components/remarkPop/remarkPop.js
const api = require('../../config/api.js');
const util = require('../../utils/util.js');
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        showRemarkPop: {
            type: Boolean,
            value: false
        },
        recordId: {
            type: String,
            value: ''
        },
        remark: {
            type: String,
            value: ''
        },
        optype: {
            type: String,
            value: 'add'
        },
        opindex: {
            type: String,
            value: '0'
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        remarkContext: '',
        autoSize: {
            maxHeight: 800,
            minHeight: 200
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        hidePop() {
            this.setData({
                showRemarkPop: false,
                remarkContext: '',
                recordId: '',
            })
        },
        stockRemark() {
            let _this = this
            let {
                remarkContext,
                optype,
                remark,
                opindex,
                recordId
            } = _this.data
            if(!remarkContext && !remark){
                wx.showToast({
                  title: '备注不能为空',
                  icon:'none'
                })
                return
            }
            util.request(api.RemarkStocks, {
                "recordId": recordId,
                "remarkContext": optype == 'add' ? remarkContext : remark,
            }).then(resp => {
                if (resp.success) {
                    wx.showToast({
                        title: optype == 'add' ? '备注添加成功' : '备注修改成功',
                    })
                    _this.setData({
                        showRemarkPop: false,
                        remarkContext: '',
                        recordId: '',
                    })
                    if (optype == 'edit') {
                        _this.updateRemark(remark,opindex)
                    }

                } else {
                    wx.showToast({
                        title: resp.message,
                        icon: 'none'
                    })
                }
            })
        },
        updateRemark(value,index) {
            this.triggerEvent("updateRemark", {
                index,
                remark: value
            })
        }
    },
})